package character.action;

import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.export.Savable;
import com.jme3.math.Vector3f;
import gameobject.construct.Construct;
import java.io.IOException;
import character.AbstractCharacter;

public abstract class CharacterAction implements Savable {
    protected float timeSinceLastExecute = 1f; //starting value, determines how quickly an action can begin
    //generic variables for subclass actions to use
    protected Vector3f targetLocation;
    protected Construct targetConstruct;
    
    /** Calls execute once per second, being executed by the given character. */
    public boolean tick(float tpf, AbstractCharacter performer) {
        timeSinceLastExecute += tpf;
        if(timeSinceLastExecute > 1f) {
            timeSinceLastExecute = 0f;
            return execute(performer);
        } else {
            return false;
        }
    }
    
    /**
     * Called once per second, this is where the action takes place.
     * Typically returns true if the action has completed.
     */
    protected abstract boolean execute(AbstractCharacter performer);
    
    @Override
    public void write(JmeExporter ex) throws IOException {
	OutputCapsule capsule = ex.getCapsule(this);
        capsule.write(targetLocation, "targetLocation", null);
        capsule.write(targetConstruct, "targetConstruct", null);
    }
    
    @Override
    public void read(JmeImporter im) throws IOException {
	InputCapsule capsule = im.getCapsule(this);
        targetLocation = (Vector3f)capsule.readSavable("targetLocation", null);
        targetConstruct = (Construct)capsule.readSavable("targetConstruct", null);
    }
}
